package com.lagou.task12;

public class StringExamTest {
    public static void main(String[] args) {
        //下面的代码会创建几个对象
//        String s1 = "abc";//一个
//        String s2 = new String("ac");//2个对象，一个在堆区，一个在常量池

        String s1 = "hello";    //常量池
        String s2 =  "hello";   //常量池
        String s3 =  new String("hello");//堆区
        String s4 =  new String("hello");//堆区

        System.out.println(s1 == s2);//true
        System.out.println(s1.equals(s2));//true
        System.out.println(s3 == s4);//false
        System.out.println(s3.equals(s4));//true
        System.out.println(s1 ==  s4);//false
        System.out.println(s1.equals(s4));//true

        System.out.println("---------------------------------");
        //常量有优化机制，变量没有
        String s5 = "abcd";
        String s6 = "ab" + "cd";//常量优化机制
        System.out.println(s5 == s6);//true

        String s7 = "ab";
        String s8 = s7 + "cd";
        System.out.println(s7 == s8);//false
    }
}
